package com.example.oms.service.impl;

import com.example.oms.dao.PatientsDao;
import com.example.oms.dao.UsersDao;
import com.example.oms.dto.ReserveOfflineDto;
import com.example.oms.entity.Patients;
import com.example.oms.entity.Users;
import com.example.oms.service.UserService;
import com.example.oms.utils.DataResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UsersDao usersDao;
    @Resource
    PatientsDao patientsDao;
    @Override
    public Users login(Users user) {
        return usersDao.queryByAccount(user.getAccount());
    }

    @Override
    public Users register(Users user) {
        user.setType("PATIENT");
        if(usersDao.queryByAccount(user.getAccount())==null){
            usersDao.insert(user);
            Patients patients=new Patients();
            patients.setId(usersDao.queryByAccount(user.getAccount()).getId());
            patients.setTel(user.getAccount());
            patientsDao.insert(patients);
            return user;
        }
        return null;
    }

    @Override
    public boolean accountIsExisted(String tel) {
        return usersDao.queryByAccount(tel) != null;
    }

    @Override
    public boolean checkPatientIdNumber(Integer id) {
        String s=patientsDao.queryById(id).getIdCardNumber();
        if(null==s||"".equals(s))return true;
        return false;
    }

    @Override
    public int creatAccount(ReserveOfflineDto reserveOfflineDto) {
        Users user = new Users();
        user.setAccount(reserveOfflineDto.getTel());
        user.setPassword("123456");
        user.setType("PATIENT");
        usersDao.insert(user);
        return user.getId();
    }

    @Override
    public void reloadpassword(String tel) {
        usersDao.updateUser(tel);
    }
    private Date parseDate(String s){
        try {
            SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
            return format.parse(s);
        }catch (Exception e){
            return new Date();
        }

    }

    @Override
    public DataResult addDetail(String idNumber, String name, HttpServletRequest request) {
        try {
            String id=request.getHeader("id");
            Date date=parseDate(idNumber.substring(6,14));
            String sex=idNumber.substring(16,17);
            if(Integer.valueOf(sex)%2==0)sex="1";
            else sex="0";
            Patients patients=patientsDao.queryById(Integer.valueOf(id));
            patients.setId(Integer.valueOf(id));
            patients.setIdCardNumber(idNumber);
            patients.setName(name);
            patients.setBirthday(date);
            patients.setSex(Integer.valueOf(sex));
            patientsDao.update(patients);
        }catch (Exception e){
         return new DataResult(201,null,"服务异常");
        }
        return new DataResult(200,null,"填写成功");
    }
    private LocalTime parseTime(String s){
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime localTime=LocalTime.parse(s,dateTimeFormatter);
        return localTime;
    }
    @Override
    public boolean allowAddQueue(Date appointmentDate, String appointmentPeriod) {
        Date date=new Date();
        if(!(date.getYear()==appointmentDate.getYear()&&date.getMonth()==appointmentDate.getMonth()&&date.getDate()==appointmentDate.getDate()))
    return false;
        String[]strings=appointmentPeriod.split("~");
        LocalTime localTime=parseTime(strings[0]);
        Duration duration=Duration.between(LocalTime.now(),localTime);
        if(duration.toMinutes()>20){
            return false;
        }
        return true;
    }
}
